package com.winz.cultureapp.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.ArtDesAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ArtDesListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.MarginDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 艺术鉴赏详情列表
 */
public class ArtAppreciateListActivity extends BaseAppCompatActivity implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.xrcv_list)
    XRecyclerView mXrcv_list;

    private boolean isRefresh = true;
    private int limit = 12;
    private int page = 1;
    private int totalPage = 0;

    private List<ArtDesListBean.DataBean.ListBean> mDatas;
    private ArtDesAdapter mAdapter;

    private Uri mPdfUriFromFile;
    private File pdfFile;

    private static final String FILE_PROVIDER_AUTHORITY = "com.winz.cultureapp.fileprovider";
    private String artId;
    private String title;

    private String artType = "0"; // text   video  audio

    @Override
    protected int contentView() {
        return R.layout.activity_art_appreciate_lsit;
    }

    @Override
    protected void initViews() {
        mDatas = new ArrayList<>();
        // XrecyclerView
        GridLayoutManager manager = new GridLayoutManager(this,3);
        mXrcv_list.addItemDecoration(new MarginDecoration(mXrcv_list,getResources().getDimensionPixelOffset(R.dimen.layout_padding_small),3));
        mXrcv_list.setLayoutManager(manager);
        mXrcv_list.setPullRefreshEnabled(true);
        mXrcv_list.setLoadingMoreEnabled(true);

        mAdapter = new ArtDesAdapter(this, mDatas, R.layout.empty_view, R.layout.item_publication);

    }

    @Override
    protected void initEvents() {

        mXrcv_list.setAdapter(mAdapter);
        mXrcv_list.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);

    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                artId = intent.getExtras().getString(Constants.KEY.ART.ITEM_ID);
                title = intent.getExtras().getString(Constants.KEY.ART.ITEM_TITLE);
                mTv_top_title.setText(title);
                getList();
            }
        }

    }

    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();
        HttpUitls.onGet("art_child", RequestFactory.getInstance().art_child(page+"",limit+"",artType,artId), new OnResponeListener<ArtDesListBean>() {
            @Override
            public void onSuccess(ArtDesListBean bean) {
                Logger.d("art_child==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    totalPage = bean.totalPage;
                    if (bean.data.list != null && bean.data.list.size() > 0) {
                        mXrcv_list.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = bean.data.list;
                        } else {
                            mDatas.addAll(bean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        ArtDesListBean.DataBean.ListBean empty = new ArtDesListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_list.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(bean.msg);
                }
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("art_child==>>", error);
                if (isRefresh) {
                    mXrcv_list.refreshComplete();
                } else {
                    mXrcv_list.loadMoreComplete();
                }
            }
        });
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_list.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            getList();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.ART.ITEM_DES_ID,mDatas.get(position).artId);
        bundle.putString(Constants.KEY.ART.ITEM_TITLE,mDatas.get(position).title);
        switch (mDatas.get(position).artType){
            case "text": // 文本
                startActivity(ArtAppreciateDesActivity.class,false,bundle);
                break;
            case "video": // 视频链接
                startActivity(ArtDesVideoActivity.class,false,bundle);
//                startActivity(PlayerActivity.class,false,bundle);
                break;
            case "audio": // 音频链接
                startActivity(ArtDesAudioActivity.class,false,bundle);
                break;
            case "otherVideo": // 网页视频链接
                startActivity(ArtDesOtherVideoActivity.class,false,bundle);
                break;
        }

    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    @OnClick(R.id.fl_back)
    public void onClick() {
        finish();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
